/**
 * Created by easy on 15/6/10.
 */
require.config({
    paths: {
        MoCoop: '../MoCoop/MoCoop',
        uuid: '../tools/uuid',
        text : '../MoCoop/components/require/text',
        json : '../MoCoop/components/require/json'
    }
});
define(['uuid',"MoCoop","json!../serviceInterface/serviceInterface.json"],function (uuid,mc,serviceConfig) {
    var service = {
        AppKey:"4c83c150-0852-11e5-be68-005056a68c0b",
        serviceConfig:serviceConfig,
        request:false
    };
    //执行AJAX请求
    service.ajaxRequest = function (params, callback) {
        service.request = true;
        var timeOut = 30;
        var serviceName = params.serviceName;
        var serviceCode = service.serviceConfig[serviceName].ServiceCode;
        var ajaxURL = service.url;
        var ajaxType = params.method;
        var time = new Date().getTime();
        time = mc.formatDate(time,"yyyyMMddhhmmssS").toString();
        var ajaxData = {
            "Service": {
                "Header": {
                    "Request": {
                        "ServiceCode": serviceCode,
                        "ReqSysCode": "S04",
                        "SerialNumber": "S04" + uuid.uuid(),
                        "ReqTime": time,
                        "ReqType": "0",
                        "Ucode": mc.userInfo.userCode,
                        "Bcode": mc.userInfo.userbCode,
                        "Security": {
                            "AppKey": service.AppKey
                        }
                    }
                },
                "Body": {
                    "Request": params.data
                }
            }
        };
        //审批接口超时180秒
        if (serviceName === "ratify") {
            timeOut = 180;
        }
        var serviceResponseCallback = function(data){
            service.request = false;
            if(data.result === true){
                //alert("直接返回的数据"+ JSON.stringify(data.response));
                var responseData = data.response;
                var code = responseData.Service.Header.Response.ReturnCode;
                if(code === "000000000"){
                    callback(responseData.Service.Body.Response);
                }else{
                    var message = responseData.Service.Header.Response.ReturnMessage;
                    mc.notification("","Close");
                    //mc.alert(message);
                    var ESBSerialNumber = responseData.Service.Header.Request.SerialNumber;
                    var logInfo = {
                        level:"Low",
                        content:message,
                        ESBSerialNumber:ESBSerialNumber,
                        ApiName:serviceCode,
                        ApiURL:ajaxURL,
                        ApiResponseBody:JSON.stringify(responseData.Service.Header.Response)
                    };
                    mc.appendLog(logInfo);
                }
            }else{
                var errorMessage = data.error.localizedDescription;
                mc.notification(errorMessage,"Info");
            }
        };
        var sendAjaxRequest = function(){
            //alert(JSON.stringify(ajaxData));
            //alert(JSON.stringify(ajaxURL));
            mc.beginAsyncAjax(ajaxURL, ajaxData, ajaxType, serviceResponseCallback, {timeOut:timeOut});
        };
        if(ajaxURL === undefined){
            mc.getServiceURL(function(data){
                service.url = data;
                ajaxURL = service.url;
                sendAjaxRequest();
            });
        }else{
            sendAjaxRequest();
        }
    };
    //期货盈亏列表
    service.getprofitlossList = function (options, callback) {
        var params = {
            "serviceName":"profitlossList",
            "method": 'POST',
            "data": options
        };
        service.ajaxRequest(params,function(data){
            mc.notification("","Close");
            callback(data);
        });
    };
    //期货持仓列表
    service.futuresPositionsList = function (options, callback) {

        var params = {
            "serviceName":"futuresPositions",
            "method": 'POST',
            "data": options
        };
        service.ajaxRequest(params,function(data){
            mc.notification("","Close");
            callback(data);
        });
    };
    //获取可用时间
    service.getRequestDate=function(options, callback){
        var params = {
            "serviceName":"availableTime",
            "method": 'POST',
            "data": options
        };
        service.ajaxRequest(params,function(data){
            mc.notification("","Close");
            callback(data);
        });
    };
    return service;
});